<?php

namespace controllers\admin;

use libs\request as request;
use libs\registry as Registry;

class kadry extends \controllers\_SkyCMS_Controller
{
    
    private $_tabela = 'users';
    
    public function __construct($options, $request) 
    {
        parent::__construct($options, $request);
        $this->uprawnieniaCheck('kadry');
    }
    
    
    /**
     * @route=(/kadry)
     */
    public function index()
    {
        $db = Registry::get('database');
        
        $users = $db->getAll($this->_tabela);
        
        $tresci = $db->getAll('strony');
        
        return array('uzytkownicy' => $users, 'tresci' => $tresci);
    }
    
    
    /**
     * @route=(/kadry/dodaj)
     */
    public function dodaj()
    {
        $db = Registry::get("database");
        
        $uprawnienia = request::post('uprawnienia');
        
        if(is_array($uprawnienia))
        {
            $uprawnienia = implode(',', $uprawnienia);
        }
        
        $haslo = md5('##'.request::post('haslo').'##');
        
        $result = $db->insert($this->_tabela,array( 'haslo'=>$haslo,
                                                    'uprawnienia'=>$uprawnienia,
                                                    'imie'=>request::post('imie'),
                                                    'nazwisko'=>request::post('nazwisko'),
                                                    'login'=>request::post('login'),
                                                    'mail'=>request::post('mail'),
                                                    'poziom'=>request::post('poziom'),
                ));
        
        header('Location: '.URL.'_SkyCMS/kadry');
        
        return array();
    }
    
        
    
    /**
     * @route=(/kadry/usun/{id})
     */
    public function usun($array)
    {
        $id = $array[0];
        
        $db = Registry::get("database");
        
        $result = $db->delete($this->_tabela,array('id'=>$id));
        $result = $db->delete('strony_seo',array('id_strony'=>$id));
        
        header('Location: '.URL.'_SkyCMS/kadry');
    }
}

?>
